<template>
    <bi-form ref="form">
        <bi-form-item label="驱动名称" >
            <bi-input v-model="driverName" required rule="^[a-zA-Z_][a-zA-Z0-9_]*$" rule-message="只能输入字母数字下划线"  :disabled="edit" ></bi-input>
        </bi-form-item>
        <bi-form-item label="驱动类型"  >
             <bi-select v-model="form.driver" required @change="flashDriver()" :option="option" ></bi-select>
        </bi-form-item>

        <bi-form-item v-for="param,name in driver" :key="name"  :label="name"     >
            <bi-input v-model="form[name]" :placeholder="param.description"  ></bi-input>
        </bi-form-item>
    </bi-form>
</template>

<script>
export default{
    data(){
        
        let form = {
            driver:''
        };
        
        let option  = [];

        for(let name in this.param){
            option.push({
                value:name.toLowerCase(),
                label:name
            })
        }

        if (this.name != ''){
            form.driver = this.item.driver;
        }
        return {
            form,
            option,
            driverName:this.name,
            edit:this.name!='',
            driver:[]
        }
    },
    props:{
        type:{
            type:String,
            default:''
        },
        app:{
            type:String,
            default:''
        },
        name:{
            type:String,
            default:''
        },
        item:{
            type:Object
        },
        param:{
            type:Object
        },
    },
    methods:{
        flashDriver(){
            
            for(let name  in this.param){
                if (name.toLowerCase() == this.form.driver){
                    this.driver =  this.param[name];
                    this.form = {driver:this.form.driver};
                    for(let key in this.driver){
                        if (this.item && this.item[key] != undefined) this.form[key] = this.item[key]; 
                        else this.form[key] = this.driver[key].default;
                    }
                    break;
                }
            }

            this.dialog.flashSize();
        },
        dialogOk(){

            if (this.$refs.form.check()){
                this.$ajax({
                    url:this.$url({app:'admin',controller:'driver',action:'save'}),
                    data:{
                        type:this.type,
                        app:this.app,
                        name:this.driverName,
                        config:this.form,
                        edit:this.edit
                    },
                    success:(data)=>{
                        this.dialog.close();
                        this.dialog.callback();
                    }
                })
            }

            return false;
        },
        
    },
    mounted(){
        if (this.edit) this.flashDriver();
    }
}
</script>